<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
require_once "../extend\org\Verify.php";

class LoginController extends Controller {
	/**
	 * 登录视图
	 */
	public function index() {
		if(session()->has("uid")){
			return IndexController::index();
		}else{
			return view('admin.login');
		}
	}

	public function login(Request $Request) {
		// 判断验证码
		$verify = new \Verify();
        if (!$verify->check($Request->code)) {
            return ['code'=>'0','msg'=>'验证码错误'];
        }
		// 判断用户名
		$user = \App\Models\User::where('user_name',$Request->user_name)->first();
		if(empty($user)){
			return ['code'=>'0','msg'=>'该用户不存在'];
		}
		$user = $user->toArray();
		// 判断密码
		if($user['passwd'] != md5($Request->passwd.env('PASSWD_SALT'))){
			return ['code'=>'0','msg'=>'密码错误'];
		}
		$this->doLogin($user);
		return ['code'=>'1','msg'=>'success'];
	}

	public function doLogin($user) {
		$data = [
			'uid' => $user['id'],
			'user_info' => $user,
		];
		session($data);
		$menu = \App\Models\Node::mkMenu();
		session(['menu' => $menu]);
	}

	public static function logout() {
		session()->flush();
		redirect('/admin')->send();
	}

	public function checkVerify(){
		$fontttfArr = [2,4,6];
		$fontttf = $fontttfArr[array_rand($fontttfArr,1)].".ttf";
        $verify = new \Verify();
        $verify->imageH = 40;
        $verify->imageW = 120;
        $verify->length = 4;
        $verify->useNoise = false;
		$verify->useCurve = true;
		$verify->fontttf = $fontttf;
        $verify->fontSize = 18;
        return $verify->entry();
    }
}
